/**
 * Created by chenhao on 2018/11/28 0028.
 */
/*
* 创建人：yangyou
* 创建时间：2017、8、28
* 描述：用户手机登陆
* */
var __HOME__="http://"+window.location.host+"/";

function usphonelogin(){
    anliuheide("#smsSubmitBtn");
    var phone,yanma;
    phone= $.trim($("#phoneIpt").val());
    yanma= $.trim($("#yanma").val());
    if(phone==""){
        $('#errorItemsj').removeClass('hidden');
        // $('#okItemsj').addClass('hidden');
        $('#errorItemsj').text("请填写您的手机号码");
        anliushow("#smsSubmitBtn","usphonelogin()");
        return false;
    }
    if(phone.length!=11||isNaN(phone)||parseInt(phone)<=0){
        $('#errorItemsj').removeClass('hidden');
        $('#okItemsj').addClass('hidden');
        $('#errorItemsj').text("您填写的手机号码格式错误");
        anliushow("#smsSubmitBtn","usphonelogin()");
        return false;
    }
    if(yanma==""){
        $('#errorItemcode').removeClass('hidden');
        $('#okItemsj').addClass('hidden');
        $('#errorItemcode').text("请填写您的手机验证码");
        anliushow("#smsSubmitBtn","usphonelogin()");
        return false;
    }
    if(yanma.length!=4||isNaN(yanma)||parseInt(yanma)<=0){
         $('#errorItemcode').removeClass('hidden');
         $('#errorItemcode').text("您填写的手机验证码格式错误");
        anliushow("#smsSubmitBtn","usphonelogin()");
         return false;
    }
    $.ajax({
        type :"POST",
        url : __HOME__+"phonelogin",
        data:{
            "phone":phone,
            "yanma":yanma
        },
        async : true,
        dataType:'json',
        success : function(result) {
            // window.parent.location.reload();
            if(result=="ok"){
                layer.alert('登陆成功欢迎您的来访',function(index){
                    //do something
                    var index2 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index2);
                    layer.close(index);
                });
            }else if(result=="ok1"){
                layer.alert('登陆成功欢迎您的来访',function(index){
                    //do something
                    var index2 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index2);
                    layer.close(index);
                });
            }else if(result=="ok2"){
                layer.alert('恭喜您首次登陆成功，欢迎您的来访',function(index){
                    //do something
                    var index2 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index2);
                    // layer.close(index);
                });
            }else{
                layer.alert(result,function(index){
                    var index2 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index2);
                    layer.close(index);
                });
                anliushow("#smsSubmitBtn","usphonelogin()");
            }
        },
        error : function() {
            alert("当前网络可能较差请稍后再试");
            anliushow("#smsSubmitBtn","usphonelogin()");
        }
    });
}
/*
* 创建人：yang you
* 创建时间：2018、12、18
* 描述：获取登陆验证码
* */
function logyanma(obj){
    var phone=$.trim($("#phoneIpt").val());
    if(phone==""){
        $('#errorItemsj').removeClass('hidden');
        $('#errorItemsj').text("请填写您的手机号码");
        return false;
    }
    var _this = $(obj);
    _this.addClass('sms-disabel-btn');
    _this.removeAttr("onclick");
    $.ajax({
        type :"POST",
        url : __HOME__+"logyanma",
        data:{
            "phone":phone
        },
        async : true,
        dataType:'json',
        success : function(result) {
            if(result=="0"){
                _this.removeClass('sms-disabel-btn');
                cfdj(5,obj);
                _this.attr("onclick","logyanma(this);");
                layer.alert('尊敬的用户，验证码已成功发送到您的手机请注意查收（本次验证码有效时间为5分钟）',function(index){
                    layer.close(index);
                });
            }else{
                _this.removeClass('sms-disabel-btn');
                _this.attr("onclick", "logyanma(this);");
                layer.alert('抱歉，短信发送失败请稍后再试',function(index){
                    layer.close(index);
                });
            }
        },
        error : function() {
            _this.removeClass('sms-disabel-btn');
            _this.attr("onclick","logyanma(this);");
            layer.alert('当前网络可能较差请稍后再试',function(index){
                layer.close(index);
            });
        }
    });
}

/* 禁止重复点击 */
function cfdj(t,obj) {
    var box = $('.on-phone');
    box.each(function () {
        var timer = null;
        var n = t;
        var _this = $(obj);
        _this.html(n + 's之后获取');
        if (!_this.hasClass('sms-disabel-btn')) {
            _this.addClass('sms-disabel-btn');
            timer = setInterval(function () {
                if (n > 0) {
                    n--;
                    _this.html(n + 's之后获取');
                } else {
                    _this.removeClass('sms-disabel-btn');
                    _this.html('获取验证码');
                    _this.attr("onclick","logyanma(this);");
                    clearInterval(timer);
                    timer = null;
                    n = t;
                }
            }, 1000);
        }
    });
}
/*
 * 描述：提交按钮移除点击事件
 * */
function anliuheide(obj){
    $(obj).removeAttr("onclick");
}
/*
 * 描述：提交按钮添加点击事件
 * */
function anliushow(obj,fun){
    $(obj).attr("onclick",fun);
}
/*
* 创建人：yang you
* 创建时间：2018、11、29
* 描述：用户密码登陆
* */
function uspasslogin(){
    anliuheide("#pwdSubmitBtn");
   var username,pass;
    username= $.trim($("#username").val());
    pass=$.trim($("#pwdIpt").val());
    if(username==""){
        alert("请填写手机");
        return false;
    }
    if(pass==""){
        alert("请填写密码");
        return false;
    }
    if(pass.length>6){
        $('#errorItempw').removeClass('hidden');
        // $('#okItempw').addClass('hidden');
        $('#errorItempw').text("密码长度过长请核对后再试！");
        anliushow("#pwdSubmitBtn","uspasslogin()");
        return false;
    }
    $.ajax({
        type :"POST",
        url : __HOME__+"index/passlogin",
        data:{
            "username":username,
            "pass":pass
        },
        async : true,
        dataType:'json',
        success : function(result) {
            if(result=="ok"){
                location.href=__HOME__+"user/index";

            }else if(result=="ok1"){
                location.href=__HOME__+"user/userinfo";
            }else{
                alert(result);
                anliushow("#pwdSubmitBtn","uspasslogin()");
            }
        },
        error : function() {
            alert("当前网络可能较差请稍后再试");
            anliushow("#pwdSubmitBtn","uspasslogin()");
        }
    });
}